package com.example.ivos.user.controller;

import com.example.ivos.base.response.JsonResult;
import com.example.ivos.user.pojo.dto.UserQuery;
import com.example.ivos.user.pojo.dto.UserSaveParam;
import com.example.ivos.user.service.UserService;
import com.example.ivos.user.mapper.UserMapper;
import com.example.ivos.user.pojo.dto.UserLoginParam;
import com.example.ivos.user.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "用户模块")
@RestController
@Validated
@Slf4j
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserService userService;


    @ApiOperation("登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("用户登录:userLoginParam="+userLoginParam);
        //调用业务层方法
        //Ctrl去接口  Ctrl+Alt去接口实现类 Ctrl+Shift去方法返回值类
        UserVO userVO = userService.login(userLoginParam);
        //将返回值添加到JsonResult中返回给前端
        return JsonResult.ok(userVO);
    }

    @ApiOperation("用户管理")
    @PostMapping("save")
    public JsonResult sava(UserSaveParam userSaveParam){
        log.debug("用户添加:userSaveParam={}",userSaveParam);
        userService.save(userSaveParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除指定用户")
    @PostMapping("del")
    public JsonResult del(Long id){
        userService.del(id);
        return JsonResult.ok();
    }

    @ApiOperation("重置密码")
    @PostMapping("update/password/{userId}")
    public JsonResult resist(@PathVariable Long userId){
        userService.update(userId);
        return JsonResult.ok();
    }

    @ApiOperation("查询用户")
    @GetMapping("select")
    public JsonResult select(UserQuery userQuery){
        List<UserVO> userVOS = userService.select(userQuery);
        return JsonResult.ok(userVOS);
    }

    @ApiOperation("开关按钮修改")
    @PostMapping("update/status/{id}/{status}")
    public JsonResult updateStatus(@PathVariable Long id,@PathVariable String status){
        log.debug("控制层修改:id={},status={}",id,status);
        userService.updateStatus(id,status);
        return JsonResult.ok();
    }

    @ApiOperation("查询审批人信息")
    @GetMapping("select/audit")
    public JsonResult selectAudit(Long parentId){
        log.debug("查询审批人信息:parentId={}",parentId);
        List<UserVO> userVOS = userService.selectAudit(parentId);
        return JsonResult.ok(userVOS);
    }

}
